package com.sixth.volunteerservicesystem.Controller.news;

import com.sixth.volunteerservicesystem.Service.NewsService;
import com.sixth.volunteerservicesystem.domain.News;
import com.sixth.volunteerservicesystem.domain.Theory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class NewsController {
    @Resource
    private JdbcTemplate jdbcTemplate;
    @Autowired
    NewsService newsService;
    @RequestMapping("/newslist")
    public String newslist(Model model){
        List<News> news = newsService.list();
        model.addAttribute("news",news);
        return "news/news";
    }


    @GetMapping("/newsdetail/{id}")
    public String theorydetail(@PathVariable("id")int id, Model model) {
        News news = newsService.Getone(id);
        int view=news.getViewcount()+1;
        String sql1 = "update news set viewcount=? where id=?";
        Object[] args1 = {view, news.getId()};
        jdbcTemplate.update(sql1, args1);
        model.addAttribute("news",news);
        return "redirect:/return1/"+id;
    }
    @GetMapping("/upnews/{id}")
    public String upquantity(@PathVariable("id")int id, Model model) {
        News news = newsService.Getone(id);
        int up=news.getThumbupquantity()+1;
        String sql = "update news set thumbupquantity=? where id=?";
        Object[] args = {up, news.getId()};
        jdbcTemplate.update(sql, args );
        model.addAttribute("news",news);
        return "redirect:/return1/"+id;
    }
    @GetMapping("/return1/{id}")
    public  String retur(Model model, @PathVariable("id")int id){
        News news = newsService.Getone(id);
        model.addAttribute("news",news);
        return "news/newsdetail";
    }

}
